package com.qfedu.empproject.controller;

import com.qfedu.empproject.entity.Emp;
import com.qfedu.empproject.service.EmpService;
import com.qfedu.empproject.service.impl.EmpServiceImpl;
import com.qfedu.empproject.utils.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@WebServlet("/showAll")
public class ShowAllController extends HttpServlet {
    private EmpService empService = new EmpServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // req取出请求的第几页
        String pageIndex = req.getParameter("pageIndex");
        if(Objects.isNull(pageIndex)){
            pageIndex = "1";
        }
        int pageIdx = Integer.valueOf(pageIndex);
        // 总共的记录数
        long totalCount = empService.getTotalCount();
        Page page = new Page(pageIdx, 10);
        page.setTotalCount(Integer.valueOf(totalCount+""));
        List<Emp> empList = empService.getEmpsByPage(page);
        // 把数据放入请求中
        req.setAttribute("empList",empList);
        req.setAttribute("page",page);
        req.getRequestDispatcher("/manager/showAllEmp.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
